﻿Imports System.ComponentModel.Design
Imports System.ComponentModel
Imports System.Runtime.InteropServices

Public Class FormTranslatable
    Inherits System.Windows.Forms.Form


#Region "Translation support"


    ''' <summary>
    ''' Globaly disables Nls translation menu on the forms derived from FormTranslatable
    ''' </summary>
    <Browsable(False)> _
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Shared Property DisableNlsTranslationMenu As Boolean = False


    <DefaultValue(True)> _
    <Description("Enables NLS system menu item to invoke NLS translation dialog")> _
    Public Overridable Property EnableNlsTranslationMenu As Boolean = True


    ' P/Invoke constants
    Private Const WM_SYSCOMMAND As Integer = 274
    Private Const MF_STRING As Integer = 0
    Private Const MF_SEPARATOR As Integer = 2048

    ' P/Invoke declarations
    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function GetSystemMenu(ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
    End Function


    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function AppendMenu(ByVal hMenu As IntPtr, ByVal uFlags As Int32, ByVal uIDNewItem As IntPtr, ByVal lpNewItem As String) As Boolean
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Private Shared Function InsertMenu(ByVal hMenu As IntPtr, ByVal uPosition As Integer, ByVal uFlags As Integer, ByVal uIDNewItem As Integer, ByVal lpNewItem As String) As Boolean
    End Function

    ' ID for the About item on the system menu
    Private SYSMENU_NLS_ID As Integer = 1


    Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)
        MyBase.OnHandleCreated(e)
        If Me.Site IsNot Nothing AndAlso Me.Site.DesignMode Then Exit Sub
        If DisableNlsTranslationMenu OrElse Not EnableNlsTranslationMenu Then Exit Sub
        ' Get a handle to a copy of this form's system (window) menu
        Dim hSysMenu As IntPtr = GetSystemMenu(Me.Handle, False)
        ' Add a separator
        AppendMenu(hSysMenu, MF_SEPARATOR, IntPtr.Zero, String.Empty)
        ' Add the About menu item
        AppendMenu(hSysMenu, MF_STRING, New IntPtr(SYSMENU_NLS_ID), "&NLS&")
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If Me.Site IsNot Nothing AndAlso Me.Site.DesignMode Then Exit Sub
        If DisableNlsTranslationMenu OrElse Not EnableNlsTranslationMenu Then Exit Sub
        ' Test if the About item was selected from the system menu
        If ((m.Msg = WM_SYSCOMMAND) _
                    AndAlso (CType(m.WParam, Integer) = SYSMENU_NLS_ID)) Then
            LocalizerComponent.ShowTranslationManagerWindow(Me, True)
        End If
    End Sub

    Protected Overrides Sub OnLoad(e As System.EventArgs)
        MyBase.OnLoad(e)
        AddNlsComponentIfNotPreset()
    End Sub

    Private Sub AddNlsComponentIfNotPreset()
        If Me.Site Is Nothing OrElse Not Me.Site.DesignMode Then Exit Sub
        If Not EnableNlsTranslationMenu Then Exit Sub
        Dim dh = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
        If dh Is Nothing Then Exit Sub
        AddHandler dh.LoadComplete, AddressOf DesignerLoaded
    End Sub

    Private Sub DesignerLoaded(s As Object, e As EventArgs)
        Dim dh = CType(s, IDesignerHost)
        RemoveHandler dh.LoadComplete, AddressOf DesignerLoaded
        For Each comp As Component In dh.Container.Components
            If comp.GetType() Is GetType(LocalizerComponent) Then
                Exit Sub
            End If
        Next
        dh.Container.Add(New LocalizerComponent(), "_NlsComponent")
    End Sub



#End Region

End Class